<?php
namespace shareTrait\Controller;
/**
 * Description of ExtraFunction
 *
 * @author hardy
 */
trait ExtraFunction
{

    /**
     * 添加
     * @param string $url 跳转链接
     */
    public function insertData($url=''){
        if (IS_POST) {
            parent::autoCheckToken();
            $data = I('post.');
            $model = D(CONTROLLER_NAME);
            $r = $model->createAdd($data);
            if($r === false){
                $this->error($model->getError());
            }
            else{
                if($url == '')
                    $this->success(l('add') . l('success'), U('index'));
                else
                    $this->success(l('add') . l('success'), $url);
            }
        }
    }
    /**
     * 编辑
     * @param string $url 跳转链接
     * @throws \Think\Exception
     */
    public function updateEdit($url=''){
        if (IS_POST) {
            parent::autoCheckToken();
            $m_id = I('post.id');
            $data = I('post.');
            $model = D(CONTROLLER_NAME);
            if(!$m_id){
                E('缺少ID');
            }
            $ent = $model->getOne($m_id);
            if(!$ent){
                E('访问的内容不存在');
            }
            if($model->createSave($data) === false){
                $this->error($model->getError());
            }
            else{
                if($url == '')
                    $this->success('修改成功', U('index'));
                else
                    $this->success('修改成功', $url);
            }
        }
    }
    /**
     * 保存排序
     */
    public function save(){
        if(IS_POST){
            $data = I('post.');
            $model = D(CONTROLLER_NAME);
            foreach($data['id'] as $k=>$v){
                $save_data['sort'] = $data['sort'][$k];
                $model->where('id=' . $v)->save($save_data);
            }
            $this->success('保存成功');
        }
    }
    /**
     * 禁用
     */
    public function forbid(){
        $ids = I('ids');
        if(!$ids){
            $this->error('请选择要禁用的数据');
        }
        $r = parent::_forbid($ids);
        if($r !== false){
            $this->success('禁用成功');
        }
        else{
            $this->error($this->_getError());
        }
    }
    /**
     * 启用
     */
    public function resume(){
        $ids = I('ids');
        if(!$ids){
            $this->error('请选择要启用的数据');
        }
        $r = parent::_resume($ids);
        if($r !== false){
            $this->success('启用成功');
        }
        else{
            $this->error($this->_getError());
        }

    }

    /**
     * 反射
     * @param $function
     * @return bool
     * @throws \ReflectionException
     */
    public function checkUnusual($function){
        $className = new \ReflectionClass($this);
        return method_exists($className->name, $function);
    }
    /**
     * 删除
     */
    public function delete(){
        $ids = I('ids');
        if(!$ids){
            $this->error('请选择要删除的数据');
        }
        //获取关联表
        if($this->checkUnusual('getRelevanceTable')){
            /*
             * 该函数的返回值如下：
             * return [
             *      ['Practice','student_id','请删除该学生的口译练习，再进行删除操作。'],
             *  ];
             */
            $deleteData = $this->getNotDeleteDataList();
            foreach ($deleteData as $value){
                if(D($value[0])->where([$value[1] => ['in',$ids]])->count()){
                    $this->error($value[2]);
                }
            }
        }
        $r = parent::_del($ids);
        if($r === false){
            $this->error($this->_getError());
        }
        else{
            $this->success('删除成功');
        }
    }
}
